﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
   public class RechargeCollection:BoundaryCollecionBase
    {
        public RechargeCollection(int nrows, int ncolumns, int nlayers, int nPeriods)
            : base(nrows, ncolumns, nlayers)
        {
            this._recharge = new Recharge(nrows, ncolumns, nlayers, nPeriods);

            //this._isUsePrecious.Add(false);

            //for (int i = 1; i < nPeriods; i++)
            //{
            //    this._isUsePrecious.Add(true);
            //}
        }


        Recharge _recharge;

        public Recharge Recharge
        {
            get { return _recharge; }
            set { _recharge = value; }
        }

        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            base._stressPeriods_StressPeriodChanged(sender,e);

            List<StressPeriod> periods = e.Param as List<StressPeriod>;

            this.Recharge.OnResetSperiods(periods.Count);

        }

        #region 公共函数

        public void SetRecharge(Recharge recharge)
        {
            this.Recharge.Option = recharge.Option;
            int i=0;
            foreach(RechargePeriod period in recharge.Periods)
            {
                this.Recharge.Periods[i].Multiplier = period.Multiplier;
                this.Recharge.Periods[i].Flux = (double[,])period.Flux.Clone();
                this.Recharge.Periods[i].Indicator = (int[,])period.Indicator.Clone();
                i++;
            }
            this.AskCollectionChanged(new SCColleconChangedEventArgs(recharge, SCCollectionChangedType.MODIFY, SCType.RECHARGE));
        }

        public PRecharge ToRechargePack()
        {
            PRecharge pr = new PRecharge();

            switch (this.Recharge.Option)
            {
                case RECHARGEOPTION.TOPLAYER: pr.NRCHOP = 1;
                    break;
                case RECHARGEOPTION.VERTICALCELLS: pr.NRCHOP = 2;
                    break;
                case RECHARGEOPTION.HIGHESTACTIVECELL: pr.NRCHOP = 3;
                    break;
            }


            //增加一个计数器，强制第一值不允许使用上一次设置

            int i = 1;
            foreach (RechargePeriod item in this.Recharge.Periods)
            {
                SPRecharge spr = new SPRecharge();
                if (pr.NRCHOP == 2)
                {
                    if (this._isUsePrecious[i -1])
                    {

                        if (i == 1)
                        {

                            spr.INIRCH = 1;
                            spr.INRECH = 1;
                            spr.IRCH = item.Indicator;
                            spr.RECH = item.Flux;


                        }
                        else
                        {


                            spr.INIRCH = -1;
                            spr.INRECH = -1;
 

                        }



                    }
                    else
                    {
                        spr.INIRCH = 1;
                        spr.INRECH = 1;
                        spr.IRCH = item.Indicator;
                        spr.RECH = item.Flux;
                    }

                }
                else
                {
                    if (this._isUsePrecious[i-1])
                    {

                        if (i == 1)
                        {
                            spr.INRECH = 1;
                            spr.RECH = item.Flux;

                        }
                        else
                        {
                            spr.INRECH = -1;

                        }

                        
                    }
                    else
                    {
                        spr.INRECH = 1;
                        spr.RECH = item.Flux;
                    }

                }
                pr.StressPeriodCharges.Add(spr);
                i++;
            }

            return pr;
        }


        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);

            this._recharge = new Recharge(nrows,ncols,nlayers,this.NPeriods);
        }

        #endregion

       
    }
}
